package cn.com.czpb.client.app;

import android.content.Context;
import android.content.pm.PackageManager;
import android.os.Environment;
import android.support.multidex.MultiDex;
import android.support.multidex.MultiDexApplication;
import android.text.TextUtils;

import cn.com.czpb.client.model.newspager.NewsPaperNode;
import cn.com.czpb.client.model.newspager.NodeEntity;
import cn.com.czpb.client.utils.LogUtils;

import com.iflytek.cloud.SpeechConstant;
import com.iflytek.cloud.SpeechUtility;
import com.mob.MobSDK;
import com.umeng.analytics.MobclickAgent;
import com.umeng.commonsdk.UMConfigure;
import com.uuzuche.lib_zxing.activity.ZXingLibrary;

import org.litepal.LitePal;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import cn.jpush.android.api.JPushInterface;

/**
 * Created by 猪猪侠 on 2017/6/30.
 */

public class App extends MultiDexApplication {
    public static NodeEntity nodeList;
    public static NewsPaperNode node;
    private static App mApp;
    public  boolean isStartApp = false;// app是否已经启动

    public static List<String> citys;

    @Override
    public void onCreate() {
        SpeechUtility.createUtility(this, SpeechConstant.APPID +"=5b30a45e");

        super.onCreate();
        mApp =this;
        LitePal.initialize(mApp);
        MobSDK.init(this);
        JPushInterface.setDebugMode(true);
        JPushInterface.init(this);
//        SDKInitializer.initialize(this);
        ZXingLibrary.initDisplayOpinion(this);

        UMConfigure.init(mApp,"5adf082e8f4a9d42b40002d4","umeng", UMConfigure.DEVICE_TYPE_PHONE, null);
        MobclickAgent.setScenarioType(mApp, MobclickAgent.EScenarioType.E_UM_NORMAL);

        citys = new ArrayList<>();
        citys.add("南宁");
        citys.add("柳州");
        citys.add("桂林");
        citys.add("梧州");
        citys.add("北海");
        citys.add("防城港");
        citys.add("钦州");
        citys.add("贵港");
        citys.add("玉林");
        citys.add("百色");
        citys.add("贺州");
        citys.add("河池");
        citys.add("来宾");
        citys.add("崇左");


    }
    public static App getInstance(){
        return mApp;
    }
    public static Context getContext(){
        return mApp.getApplicationContext();

    }
    public String getAllDiskCacheDir(){
        String cachePath = "" ;
        String state = Environment.getExternalStorageState();
        if (Environment.MEDIA_MOUNTED.equals(state)){
            File externalFile=getApplicationContext().getExternalCacheDir();
            if((null!=externalFile)&&(!externalFile.exists()||externalFile.mkdir())){
                cachePath=externalFile.getPath() ;
            }
        }

        if(TextUtils.isEmpty(cachePath)){
            File dir=getApplicationContext().getCacheDir();
            if(!dir.exists()){
                dir.mkdir();
            }
            cachePath=dir.getPath();
        }

        return cachePath;
    }
    public String getJsonFileCacheRootDir(){
        String cachePath =getAllDiskCacheDir()+ File.separator + "jsonfile";
        File file =new File(cachePath);
        if(!file.exists()){
            file.mkdirs();
        }
        return file.toString();
    }
    public static File getFile(String path){

        int fp=path.lastIndexOf(File.separator);
        String sfp=path.substring(0, fp);
        LogUtils.i("spf-->"+sfp);
        File fpath=new File(sfp);

        if(!fpath.exists()&&!fpath.isDirectory()){
            fpath.mkdirs();
        }

        File f=new File(path);
        if(!f.exists()){
            try {
                f.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return f;
    }
    public static String getFileContext(File file){
        FileReader fileReader;
        try {
            fileReader = new FileReader(file);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            return null;
        }

        BufferedReader bufReader=new BufferedReader(fileReader);
        StringBuilder sb=new StringBuilder();
        try {
            String data;
            while((data=bufReader.readLine())!=null){
                sb.append(data);
            }
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }finally{
            try {
                bufReader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return sb.toString();
    }
    @Override
    protected void attachBaseContext(Context base) {
        MultiDex.install(this);
        super.attachBaseContext(base);
    }

    public static String getVerName(Context context) {
        String verName = "";
        try {
            verName = context.getPackageManager().
                    getPackageInfo(context.getPackageName(), 0).versionName;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return verName;
    }

}
